package mylist;

import java.util.Arrays;

public class MyArrayList implements IList {

    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 2;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArrayList(int capacity) {
        this.elem = new int[capacity];
    }

    @Override
    public void add(int data) {
        checkCapacity();

        this.elem[usedSize] = data;
        usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPosOnAddAndSet(pos);
        } catch (PosIllegality e) {
            e.printStackTrace();
            return;
        }
        checkCapacity();
        for (int i = usedSize; i > pos; i--) {
            elem[i] = elem[i - 1];
        }
        elem[pos] = data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        if (isEmpty()) {
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws MyArrayListEmpty {
        checkPosOnGet(pos);
        if (isEmpty()) {
            throw new MyArrayListEmpty("获取指定下标元素时，顺序表为空！");
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnAddAndSet(pos);

        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("没有这个数字！");
            return;
        }
        for (int i = index; i < usedSize - 1; i++) {
            elem[i] = elem[i + 1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }

    @Override
    public boolean isEmpty() {
        return usedSize == 0;
    }

    //检查容量扩容
    private void checkCapacity() {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
    }

    //检查pos的合法性
    private void checkPosOnAddAndSet(int pos) throws PosIllegality {
        if (pos < 0 || pos > usedSize) {
            System.out.println("不合法！");
            throw new PosIllegality("插入元素下标异常 " + pos);
        }
    }

    //检查pos的合法性
    private void checkPosOnGet(int pos) throws PosIllegality {
        if (pos < 0 || pos >= usedSize) {
            System.out.println("不合法！");
            throw new PosIllegality("获取指定下标的元素异常 " + pos);
        }
    }
}
